const express = require('express');

const router = express.Router();

const Order = require('../model/order')
const Flight=require('../model/flight')

// 添加预订单
router.post('/preOrder', async (req, res) => {
    // uid不能通过客户发送过来
    // uid都应该从token中获取
    const { phone,
        startCity,
        arriveCity,
        startStationId,
        arriveStationId,
        orderDate,
        linkMan,
        flightNum
    } = req.body;

    // 数据过滤
    if (/^1(3456789)\d{9}$/.test(phone)) return res.send({ success: false, info: '手机号码有误' });
    if (!startCity) return res.send({ success: false, info: '请选择起点城市' });
    if (!arriveCity) return res.send({ success: false, info: '请选择终点城市' });
    if (!startStationId) return res.send({ success: false, info: '请选择上车站点' });
    if (!arriveStationId) return res.send({ success: false, info: '请选择下车站点' });
    if (!orderDate) return res.send({ success: false, info: '请选择出发时间' });
    if (!linkMan) return res.send({ success: false, info: '请添加至少一个乘车人' });
    
    // 暂时先写uid=1 todo 之后补全使用token的方式来获取uid
    const uid = 1;

    // 先查询 线路信息 得到线路信息然后才能计算总价
    const flight = await Flight.findOne({ flightNum })
 
    // linkMan.length 乘车人数量
    const amount =flight.ticketPrice * linkMan.length; 


    const ip=req.ip //todo

    try {
        // 执行入库操作
       const order= await Order.create({
            phone,
            startCity,
            arriveCity,
            startStationId,
            arriveStationId,
            orderDate,
            linkMan,
            flightNum,
            uid,
            amount,
            ip,
            payAt: null,
            checkDate: null,
            orderState:1
        });
        res.send({ success: true, info: '添加成功',data:order._id });
    } catch (error) {
        console.log(error)
        res.send({ success: false, info: '添加失败' });
    }

})

// 用户查询自己的全部订单
router.post('/getAll', async (req, res) => {

    let { sdate, edate, page, limit } = req.body;
    page = page || 1; //当前第几页
    limit = limit || 20; //单页返回的条数限制

    // 暂时先写uid=1 todo 之后补全使用token的方式来获取uid
    const uid = 1

    let where = {uid};
    if (sdate && !edate) where.createdAt = {$gt:sdate};
    if (!sdate && edate) where.ceratedAt = {$lt:edate};
    if (sdate && edate) where.ceratedAt = {$and:[{$gt:sdate},{$lt:edate}]};

    const skip = (page - 1) * limit; //查询的起点（偏移量）
    
    try {
        let orders = await Order.find(where, {}, { skip, limit }) //分页查询
        let count=await Order.count(where)  //获取符合条件的总数

        res.send({success:true,info:'查询成功',data:orders,count})
    } catch (error) {
        console.log(error);
        res.send({success:false,info:'查询失败'})
    }
})

// 查询单个订单的信息
router.post('/getOne', async (req, res) => {
    const { orderId } = req.body;
    if (!orderId) return res.send({ success: false, info: '请传入orderId' })
    
    try {
        const order = await Order.findById(orderId);
        res.send({success:true,info:'获取成功',data:order})
    } catch (error) {
        console.log(error);
        res.send({success:false,info:'获取失败'})
    }
})

// 修改订单状态
router.post('/changeOrder', async (req, res) => {
    const { orderId,State } = req.body;
    if (!orderId) return res.send({ success: false, info: '请传入orderId' })
     if (!State) return res.send({ success: false, info: '请传入State'})
    
    try {
        const order = await Order.findById(orderId);
        const { orderState } = order
        if (State == orderState) return res.send({ success: true, info: '不能修改为当前状态' })
        await Order.findByIdAndUpdate({ _id: orderId }, { orderState: State })
        res.send({success:false,info:'修改成功'})
    } catch (error) {
        console.log(error); 
        res.send({success:false,info:'修改失败'})
    }
})

module.exports = router;